oUF = ns.oUF or oUF
if not oUF then end

local myclass = select(2, UnitClass("player"))

local module = {}
module.name = "Own Unit Frames"
module.Init = function()
    if not SCDB.modules[module.name] then return end
    if SCDB[module.name] == nil then SCDB[module.name] = {} end
	if SCDB[module.name]["Healthbar color"] == nil then SCDB[module.name]["Healthbar color"] = {26/225, 34/225, 38/225} end
	if SCDB[module.name]["3D portraits"] == nil then SCDB[module.name]["3D portraits"] = false end
	
	local config = {
		player = { width = 180, height = 50	},
		target = { width = 180, height = 50	},
		focus = { width = 110, height = 50 },
		focustot = { width = 110, height = 50 },
		tot = { width = 100, height = 50 },
		pet = { width = 100, height = 50 },
	}	
	
	local colors = {
		power = {
			[0] = { r = 48/255, g = 113/255, b = 191/255}, -- Mana
			[1] = { r = 255/255, g = 1/255, b = 1/255}, -- Rage
			[2] = { r = 255/255, g = 178/255, b = 0}, -- Focus
			[3] = { r = 1, g = 1, b = 34/255}, -- Energy
			[5] = {	r = .55, g = .57, b = .61}, --Runes
			[6] = { r = 1, g = 0, b = 34/255}, -- Runic Power
			[7] = { r = .8, g = .6, b = 0}, --Ammoslot
			[8] = { r = 0, g = .55, b = .5}, --Fuel
			[9] = { r = .55, g = .57, b = .61}, --Steam
			[10] = { r = .60, g = .09, b = .17}, --Pyrite
		},
		health = {
			[0] = { r=SCDB[module.name]["Healthbar color"][0], g=SCDB[module.name]["Healthbar color"][1], b=SCDB[module.name]["Healthbar color"][2] } or {r = 26/225, g = 34/225, b = 38/225}, -- Health
			[1] = {r = .6, g = .6, b = .6}, -- Tapped
		},
	}
	
	local function fixvalue(val)
	   if(val >= 1e6) then
		  return ('%.2fm'):format(val / 1e6):gsub('%.?0+([km])$', '%1')
	   elseif(val >= 1e4) then
		  return ('%.1fk'):format(val / 1e3):gsub('%.?0+([km])$', '%1')
	   else
		  return val
	   end
	end
	
	oUF.TagEvents['namecolor'] = 'UNIT_NAME_UPDATE'
	oUF.Tags['namecolor'] = function(unit)
		local reaction = UnitReaction(unit, "player")
		local _, x = UnitClass(unit)
		if not UnitIsPlayer(unit) or not UnitIsFriend(unit, "player")
			then return Hex(oUF.colors.reaction[reaction])
		elseif (x)
			then return Hex(_COLORS.class[x])
		else return
		end
	end
	
	oUF.TagEvents['levelequal'] = 'UNIT_LEVEL'
	oUF.Tags['levelequal'] = function(unit)
		local c = UnitClassification(unit)
			if(c == 'worldboss') then
				return Hex(245/255, 144/255, 0) .. '+'
			elseif (c == 'elite' or c == 'rareelite')  then
				return UnitLevel(unit) .. "+"
			elseif UnitLevel(unit) == UnitLevel("player") then 
				return
			else 
				return UnitLevel(unit)
			end
	end	
	
	local function UpdateHealth(bar, unit, min, max)
		if (bar:GetParent().unit ~= unit) then return end
		local color
		bar:SetMinMaxValues(0, max); bar:SetValue(min)
	  
		if (unit:find("pet")) then
			color = colors.health[0]
		elseif (UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) or not UnitIsConnected(unit)) then
			color = colors.health[1]
		else
			color = colors.health[0]
		end
		
		if color then
			bar:SetStatusBarColor(color.r, color.g, color.b)
			if (bar.bg) then
				color = UnitIsPlayer(unit) and RAID_CLASS_COLORS[select(2, UnitClass(unit))] or FACTION_BAR_COLORS[UnitReaction(unit, "player")]
				if color then 
					bar.bg:SetVertexColor(color.r, color.g, color.b, 0.9)
				else
					bar.bg:SetVertexColor(1,1,1,0.3)
				end
			end
		end
			
		local c, d = max - min, floor(min/max*100)
		
		bar.value:SetTextColor(1,1,1)
		
		if UnitIsPlayer(unit) then
			local color = UnitIsPlayer(unit) and RAID_CLASS_COLORS[select(2, UnitClass(unit))]
			if color then bar.value:SetTextColor(color.r*.6, color.g*.6, color.b*.6) end
		end
		
		bar.value:SetAlpha(.5)
		
		if (UnitIsDead(unit)) then
			bar.value:SetText("Dead")
			bar.value:SetTextColor(0.6, 0.6, 0.6)
			bar.bg:SetVertexColor(1,1,1,0.3)
		elseif (UnitIsGhost(unit)) then
			bar.value:SetText("Ghost")
			bar.value:SetTextColor(0.6, 0.6, 0.6)
			bar.bg:SetVertexColor(1,1,1,0.3)
		elseif (not UnitIsConnected(unit)) then
			bar.value:SetText("Offline")
			bar.value:SetTextColor(0.6, 0.6, 0.6)
			bar.bg:SetVertexColor(1,1,1,0.3)
		elseif (UnitIsEnemy("player", unit)) then
			if (c > 0) then
				bar.value:SetText(fixvalue(max-c))
			else
				bar.value:SetText(fixvalue(max))
			end
		elseif (unit == "pet")then
			if (c > 0) then
				bar.value:SetText(fixvalue(max-c).." / "..fixvalue(max))
			else
				bar.value:SetText(fixvalue(max))
			end
		elseif (UnitIsPlayer(unit)) then
			bar.value:SetText(fixvalue(min)..(GetCVarBool("statusTextPercentage") and "/"..d.."%" or ""))
		else
			bar.value:SetText(d.."%")
		end
	end

	local function UpdatePower(bar, unit, min, max)
		if (bar:GetParent().unit ~= unit) then return end
		local color
		local c, d = max - min, floor(min/max*100)
		
		bar:SetMinMaxValues(0, max); bar:SetValue(min)
		color = colors.power[UnitPowerType(unit)]
	  
		if (unit == "pet") then
			color = colors.power[UnitPowerType(unit)]
		end

		if (color) then
			bar:SetStatusBarColor(color.r*.6, color.g*.6, color.b*.6)
			if (bar.bg) then
				bar.bg:SetVertexColor(color.r*.2, color.g*.2, color.b*.2)
			end
		end
		
		bar.value:SetTextColor(1, 1, 1)
	  
		if (UnitIsTapped(unit) and not UnitIsTappedByPlayer(unit) or not UnitIsConnected(unit)) then
			bar.value:SetTextColor(.5, .5, .5)	
		end
		
		if UnitIsPlayer(unit) then
			color = UnitIsPlayer(unit) and RAID_CLASS_COLORS[select(2, UnitClass(unit))]
			if color then 
				bar.value:SetTextColor(color.r*.6, color.g*.6, color.b*.6) 
			end
		end
		
		bar.value:SetAlpha(.5)
		
		if (min < 0) then
			bar.value:SetText()
		elseif (UnitIsDead(unit)) then
			bar.value:SetText()
		elseif (UnitIsGhost(unit)) then
			bar.value:SetText()
		elseif (not UnitIsConnected(unit)) then
			bar.value:SetText()
		elseif (unit == "player") then
			bar.value:SetText(fixvalue(min))
		else
			if ((max - min) > 0) then
				bar.value:SetText(fixvalue(max-(max - min)))
			else
				bar.value:SetText(fixvalue(max))
			end
		end
	end
	
	local PostUpdateAuraIcon = function(icons, unit, icon, index, offset, filter, isDebuff, duration, timeLeft)
		local _, _, _, _, debuffType, duration, expirationTime, unitCaster, isStealable = UnitAura(unit, index, icon.filter)
		if(icon.debuff) then
			if(not UnitIsFriend("player", unit) and icon.owner ~= "player" and icon.owner ~= "vehicle") then
				icon.icon:SetDesaturated(true)
				icon:SetAlpha(.5)
			else
				icon.icon:SetDesaturated(false)
				icon:SetAlpha(1)
			end
		else
			if (isStealable or ((myclass == "MAGE" or myclass == "DRUID" or myclass == "PRIEST" or myclass == "SHAMAN") and debuffType == "Magic")) and not UnitIsFriend("player", unit) then
				icon.overlay:SetVertexColor(1, 0.85, 0, 1)
			else
				icon.overlay:SetVertexColor(1,1,1)
			end		
		end
	end
	
	
	local PostCreateAuraIcon = function(auras, button)
		button.icon:SetAllPoints(button)
		button.icon:SetTexCoord(.09, .97, .09, .97)
		button.icon:SetPoint("TOPLEFT", button, "TOPLEFT", 1, -1)
		button.icon:SetPoint("BOTTOMRIGHT", button, "BOTTOMRIGHT", -1, 1)

		button.cd:SetReverse()
		button.cd:SetAllPoints(button.icon)

		button.count:ClearAllPoints()
		button.count:SetPoint("TOPLEFT", 1, -1)
		button.count:SetFontObject(NumberFontNormal)
		
		button.overlay.Hide = function(self)
			self:SetTexture("Interface\\Buttons\\UI-TotemBar")
			self:SetPoint("TOPLEFT", -1, 1)
			self:SetPoint("BOTTOMRIGHT", 1, -1)
			self:SetTexCoord(1 / 128, 35 / 128, 207 / 256, 240 / 256)
			self:SetDesaturated(1)
			self:SetVertexColor(1,1,1)
		end
		
		if(not isDebuff and button:GetParent().unit=="player") then     -- Cancel buffs on right click
			button:SetScript("OnMouseUp", CancelBuff)
		end
	end
	
	----------------------------------------------------------------------------------------
	----------------------------------LAYOUT------------------------------------------------
	----------------------------------------------------------------------------------------

	local tex = "Interface\\Addons\\"..addonName.."\\media\\texture\\statusbar"
	  
	local function menu(self)
		local dropdown = _G[string.format('%sFrameDropDown', string.gsub(self.unit, '(.)', string.upper, 1))]

		if dropdown then
			ToggleDropDownMenu(1, nil, dropdown, 'cursor')
		elseif (self.unit:match('party')) then
			ToggleDropDownMenu(1, nil, _G[format('PartyMemberFrame%dDropDown', self.id)], 'cursor')
		else
			FriendsDropDown.unit = self.unit
			FriendsDropDown.id = self.id
			FriendsDropDown.initialize = RaidFrameDropDown_Initialize
			ToggleDropDownMenu(1, nil, FriendsDropDown, 'cursor')
		end
	end
	  
	local func = function(self, unit)
		self.numBuffs = 36
		self.menu = menu

		self:EnableMouse(true)
	  
		self:SetScript("OnEnter", UnitFrame_OnEnter)
		self:SetScript("OnLeave", UnitFrame_OnLeave)
	  
		self:RegisterForClicks("anyup")
		self:SetAttribute("*type2", "menu")
	  
		self:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8x8", edgeFile = "Interface\\Buttons\\WHITE8x8",  tiled = false, })
		self:SetBackdropColor(.3, .3, .3, .5)
		self:SetBackdropBorderColor(0, 0, 0, 1)
	  
		local healthbar = CreateFrame("StatusBar", nil, self)
		healthbar:SetStatusBarTexture(tex)
		healthbar:GetStatusBarTexture():SetHorizTile(false)
		healthbar:SetPoint("TOPLEFT", 1, -1)
		healthbar:SetPoint("BOTTOMRIGHT", -1, 6)
	  
		local healthbarBackground = healthbar:CreateTexture(nil, "BORDER")
		healthbarBackground:SetAllPoints(healthbar)
		healthbarBackground:SetTexture(tex)
		healthbarBackground:SetHorizTile(false)
		healthbarBackground:SetBlendMode("BLEND")
	  
		local healthbarString = healthbar:CreateFontString(nil, "OVERLAY")
		healthbarString:SetPoint("TOPRIGHT", -3, -6)
		healthbarString:SetFontObject(GameFontHighlight)
		healthbarString:SetTextColor(1, 1, 1)
	  
		healthbar.PostUpdate = UpdateHealth
		healthbar.bg = healthbarBackground
		healthbar.value = healthbarString
		self.Health = healthbar
	  
		local powerbar = CreateFrame("StatusBar", nil, self)
		powerbar:SetStatusBarTexture(tex)
		powerbar:GetStatusBarTexture():SetHorizTile(false)
		powerbar:SetPoint("TOPLEFT", healthbar, "BOTTOMLEFT", 0, 0)
		powerbar:SetPoint("BOTTOMRIGHT", healthbar, "BOTTOMRIGHT", 0, -5)
		powerbar:SetFrameLevel(healthbar:GetFrameLevel()+2)
		
		local powerbarBackground = powerbar:CreateTexture(nil, "BORDER")
		powerbarBackground:SetAllPoints(powerbar)
		powerbarBackground:SetTexture(tex)
		powerbarBackground:SetHorizTile(false)
		powerbarBackground:SetBlendMode("BLEND")
		
		local powerbarString = powerbar:CreateFontString(nil, "OVERLAY")
		powerbarString:SetPoint("RIGHT", healthbarString, "LEFT", 2, 0)
		powerbarString:SetFontObject(GameFontHighlight)
		powerbarString:SetTextColor(1, 1, 1)
		  
		powerbar.PostUpdate = UpdatePower
		powerbar.value = powerbarString
		powerbar.bg = powerbarBackground
		self.Power = powerbar
	  
		local name = healthbar:CreateFontString(nil, "OVERLAY")
		name:SetPoint("TOPLEFT", healthbar, 3, -6)
		name:SetPoint("RIGHT", powerbarString, "LEFT", -30, 0)
		name:SetJustifyH("LEFT")
		name:SetFontObject(GameFontHighlight)
		
		if (unit=='target') then
			self:Tag(name, "[difficulty][levelequal] [namecolor][name]")
		elseif (unit=='player') then
		else
			self:Tag(name, "[namecolor][name]")
		end
		
		local portrait
		if SCDB[module.name]["3D portraits"] == true then
			portrait = CreateFrame("PlayerModel", self:GetName().."_Portrait", self)
		else 
			portrait = self:CreateTexture(self:GetName().."_Portrait", 'OVERLAY')
		end
		portrait:SetSize(config.player.height-2, config.player.height-2)
		if unit == "player" then
			portrait:SetPoint("TOPLEFT", self, "TOPLEFT", 1, -1)
			healthbar:SetPoint("TOPLEFT", portrait, "TOPRIGHT", 1, 0)
		elseif unit == "target" then
			portrait:SetPoint("TOPRIGHT", self, "TOPRIGHT", -1, -1)
			healthbar:SetPoint("BOTTOMRIGHT", portrait, "BOTTOMLEFT", -1, 5)
		end
		
		portrait.PostUpdate = function(self, unit) 
			if self:GetObjectType() ~= 'Texture' and self:GetModel() and self:GetModel().find and self:GetModel():find("worgenmale") then 
				self:SetCamera(1) 
			end 
			
			if self:GetObjectType() == 'Texture' then
				self:SetTexCoord(0,1,0,1)
				if (UnitIsPlayer(unit) and UnitExists(unit) and UnitIsConnected(unit) and unit~="pet") then
					if select(2,UnitClass(unit)) == "DRUID" then
						local i, flag=1, false
						while UnitBuff(unit, i) do
							local id = select(11,UnitBuff(unit, i))
							if id == 768 or id == 783 or id == 1066 or id == 5487 or id == 24858 or id == 33891 or id == 33943 or id == 40120 then
								self:SetTexture(GetSpellTexture(id))
								self:SetTexCoord(.1,.9,.1,.9)
								flag = true
							end
							i = i + 1
						end
						if not flag then
							self:SetTexture([[Interface\Glues\CharacterCreate\UI-CharacterCreate-Classes]])
							local left, right, top, bottom = unpack(CLASS_ICON_TCOORDS.DRUID)
							left = left + (right - left) * 0.07; right = right - (right - left) * 0.07; top = top + (bottom - top) * 0.07; bottom = bottom - (bottom - top) * 0.07
							self:SetTexCoord(left, right, top, bottom)
						end
					else
						local t = CLASS_ICON_TCOORDS[select(2,UnitClass(unit))]
						if t then
							self:SetTexture([[Interface\Glues\CharacterCreate\UI-CharacterCreate-Classes]])
							local left, right, top, bottom = unpack(t)
							left = left + (right - left) * 0.07; right = right - (right - left) * 0.07; top = top + (bottom - top) * 0.07; bottom = bottom - (bottom - top) * 0.07
							self:SetTexCoord(left, right, top, bottom)
						end
					end
				end
			end
		end
		
		self.Portrait = portrait		
		
		if config[unit] then
			local mhpb = CreateFrame('StatusBar', nil, self.Health)
			mhpb:SetPoint('TOPLEFT', self.Health:GetStatusBarTexture(), 'TOPRIGHT', 0, 0)
			mhpb:SetPoint('BOTTOMLEFT', self.Health:GetStatusBarTexture(), 'BOTTOMRIGHT', 0, 0)
			mhpb:SetWidth(config[unit].width)
			mhpb:SetStatusBarTexture(tex)
			mhpb:SetStatusBarColor(0, 1, 0.5, 0.45)
			mhpb:SetMinMaxValues(0,1)

			local ohpb = CreateFrame('StatusBar', nil, self.Health)
			ohpb:SetPoint('TOPLEFT', mhpb:GetStatusBarTexture(), 'TOPRIGHT', 0, 0)
			ohpb:SetPoint('BOTTOMLEFT', mhpb:GetStatusBarTexture(), 'BOTTOMRIGHT', 0, 0)
			ohpb:SetWidth(config[unit].width)
			ohpb:SetStatusBarTexture(tex)
			ohpb:SetStatusBarColor(0, 1, 0, 0.45)

			self.HealPrediction = {
				myBar = mhpb,
				otherBar = ohpb,
				maxOverflow = 1,
			}
		end		
		
		if (unit=='player') then
			if myclass =='DRUID' then
				local eclipseBar = CreateFrame('Frame', nil, self)
				eclipseBar:SetPoint("BOTTOM", self, "TOP", 0, 2)
				eclipseBar:SetSize(config.player.width-2, 9)
				eclipseBar:SetBackdrop{ bgFile = "Interface\\BUTTONS\\WHITE8X8", tile = true, tileSize = 16, insets = {left = -1, right = -1, top = -1, bottom = -1}, }
				eclipseBar:SetBackdropColor(0,0,0,1)
				
				local EclipseDisplay = function(self) 
					local eb, icon = self.EclipseBar, self.EclipseBar.Icon
					if eb:IsShown() then 
						icon:Show() 
					else 
						icon:Hide() 
					end
				end	  
				eclipseBar:SetScript("OnShow", function() EclipseDisplay(self) end)
				eclipseBar:SetScript("OnUpdate", function() EclipseDisplay(self) end)
				eclipseBar:SetScript("OnHide", function() EclipseDisplay(self) end)

				local lunarBar = CreateFrame('StatusBar', nil, eclipseBar)
				lunarBar:SetPoint('TOPLEFT')
				lunarBar:SetSize(config.player.width-2, 9)
				lunarBar:SetStatusBarTexture(tex)
				lunarBar:SetStatusBarColor(.30, .52, .90)
				eclipseBar.LunarBar = lunarBar

				local solarBar = CreateFrame('StatusBar', nil, eclipseBar)
				solarBar:SetPoint('TOPLEFT', lunarBar:GetStatusBarTexture(), 'TOPRIGHT', 0, 0)
				solarBar:SetSize(config.player.width-2, 9)
				solarBar:SetStatusBarTexture(tex)
				solarBar:SetStatusBarColor(.80, .82, .60)
				eclipseBar.SolarBar = solarBar

				local eclipseBarIcon = CreateFrame('frame', nil, solarBar)
				eclipseBarIcon:SetPoint('BOTTOM', eclipseBar, 'TOP', 0, 1)
				eclipseBarIcon:SetSize(22, 22)
				eclipseBarIcon.icon = eclipseBarIcon:CreateTexture(nil, 'OVERLAY')
				eclipseBarIcon.icon:SetAllPoints(eclipseBarIcon)
				eclipseBar.PostUpdatePower = function(self)
					if ( GetEclipseDirection() == "sun" ) then
						eclipseBarIcon.icon:SetTexture("Interface\\ICONS\\spell_arcane_starfire")
					elseif ( GetEclipseDirection() == "moon" ) then
						eclipseBarIcon.icon:SetTexture("Interface\\ICONS\\spell_nature_abolishmagic")
					else
						eclipseBarIcon.icon:SetTexture(nil)
					end
				end

				eclipseBar.PostUnitAura = function(self, unit)
					if self.hasSolarEclipse or self.hasLunarEclipse then
						ActionButton_ShowOverlayGlow(self.Icon)
					else
						ActionButton_HideOverlayGlow(self.Icon)
					end
				end
				self.EclipseBar = eclipseBar
				self.EclipseBar.Icon = eclipseBarIcon
			end
			
			local altpowerbar = CreateFrame("StatusBar", self:GetName().."_altpowerbar", self.Health)
			altpowerbar:SetStatusBarTexture(tex)
			altpowerbar:SetPoint("TOPLEFT", powerbar, "TOPLEFT", 0, 6)
			altpowerbar:SetPoint("BOTTOMRIGHT", powerbar, "TOPRIGHT", 0, 0)
			self.AltPowerBar = altpowerbar
			
			if (myclass == "WARLOCK" or myclass == "PALADIN") then
				local bars = CreateFrame("Frame", nil, self)
				bars:SetPoint("BOTTOM", self, "TOP", 0, 2)
				bars:SetSize(config.player.width-2, 9)
				bars:SetBackdrop{ bgFile = "Interface\\BUTTONS\\WHITE8X8", tile = true, tileSize = 16, insets = {left = -1, right = -1, top = -1, bottom = -1}, }
				bars:SetBackdropColor(0,0,0,1)
				
				for i = 1, 3 do					
					bars[i]=CreateFrame("StatusBar", self:GetName().."_Shard"..i, self)
					bars[i]:SetStatusBarTexture(tex)
					bars[i]:GetStatusBarTexture():SetHorizTile(false)

					bars[i].bg = bars[i]:CreateTexture(nil, 'BORDER')
					
					if myclass == "WARLOCK" then
						bars[i]:SetStatusBarColor(255/255,101/255,101/255)
						bars[i].bg:SetTexture(255/255,101/255,101/255)
					elseif myclass == "PALADIN" then
						bars[i]:SetStatusBarColor(228/255,225/255,16/255)
						bars[i].bg:SetTexture(228/255,225/255,16/255)
					end
					
					if i == 1 then
						bars[i]:SetSize(bars:GetWidth()/3, 9)
						bars[i]:SetPoint("LEFT", bars)
						bars[i].bg:SetAllPoints(bars[i])
					else
						bars[i]:SetSize(bars:GetWidth()/3-1, 9)
						bars[i]:SetPoint("LEFT", bars[i-1], "RIGHT", 1, 0)
						bars[i].bg:SetAllPoints(bars[i])
					end
					
					bars[i].bg:SetTexture(tex)					
					bars[i].bg:SetAlpha(.15)
				end
				
				if myclass == "WARLOCK" then
					bars.Override = function(self, event, unit, powerType)
						if(self.unit ~= unit or (powerType and powerType ~= 'SOUL_SHARDS')) then return end
						local num = UnitPower(unit, SPELL_POWER_SOUL_SHARDS)
						for i = 1, SHARD_BAR_NUM_SHARDS do
							if(i <= num) then
								self.SoulShards[i]:SetAlpha(1)
							else
								self.SoulShards[i]:SetAlpha(.2)
							end
						end
					end
					self.SoulShards = bars
				elseif myclass == "PALADIN" then
					bars.Override = function(self, event, unit, powerType)
						if(self.unit ~= unit or (powerType and powerType ~= 'HOLY_POWER')) then return end
						local num = UnitPower(unit, SPELL_POWER_HOLY_POWER)
						for i = 1, MAX_HOLY_POWER do
							if(i <= num) then
								self.HolyPower[i]:SetAlpha(1)
							else
								self.HolyPower[i]:SetAlpha(.2)
							end
						end
					end
					self.HolyPower = bars
				end
			end

			if myclass == "DEATHKNIGHT" then
				local Runes = CreateFrame("Frame", nil, self)
				Runes:SetPoint("BOTTOM", self, "TOP", 0, 1)
				Runes:SetSize(config.player.width-2, 9)
				
				for i = 1, 6 do
					Runes[i] = CreateFrame("StatusBar", self:GetName().."_Runes"..i, Runes)
					if (i == 1) then
						Runes[i]:SetSize(Runes:GetWidth()/6-1, 9)
						Runes[i]:SetPoint("LEFT", 0, 1)
					else
						Runes[i]:SetSize(Runes:GetWidth()/6, 9)
						Runes[i]:SetPoint("TOPLEFT", Runes[i-1], "TOPRIGHT", 1, 0)
					end
					Runes[i]:SetStatusBarTexture(tex)
					Runes[i]:GetStatusBarTexture():SetHorizTile(false)
					Runes[i]:SetBackdrop{ bgFile = "Interface\\BUTTONS\\WHITE8X8", tile = true, tileSize = 16, insets = {left = -1, right = -1, top = -1, bottom = -1}, }
					Runes[i]:SetBackdropColor(0,0,0,1)
				end

				self.Runes = Runes
			end			
			
			if myclass == "SHAMAN" then
				local TotemBar = CreateFrame("Frame", nil, self)
				TotemBar:SetPoint("BOTTOM", self, "TOP", 0, 1)
				TotemBar:SetSize(config.player.width-2, 9)
				
				TotemBar.Destroy = false
				for i = 1, 4 do
					TotemBar[i] = CreateFrame("StatusBar", self:GetName().."_TotemBar"..i, TotemBar)
					if i == 1 then
						TotemBar[i]:SetSize(TotemBar:GetWidth()/4, 9)
						TotemBar[i]:SetPoint("LEFT", 0, 0)
					else
						TotemBar[i]:SetSize(TotemBar:GetWidth()/4-1, 9)
						TotemBar[i]:SetPoint("TOPLEFT", TotemBar[i-1], "TOPRIGHT", 1, 0)
					end
					TotemBar[i]:SetStatusBarTexture(tex)
					TotemBar[i]:SetBackdrop{ bgFile = "Interface\\BUTTONS\\WHITE8X8", tile = true, tileSize = 16, insets = {left = -1, right = -1, top = -1, bottom = -1}, }
					TotemBar[i]:SetBackdropColor(0,0,0,1)
					TotemBar[i]:SetMinMaxValues(0, 1)

					TotemBar[i].bg = TotemBar[i]:CreateTexture(nil, "BORDER")
					TotemBar[i].bg:SetAllPoints(TotemBar[i])
					TotemBar[i].bg:SetTexture(tex)
					TotemBar[i].bg.multiplier = 0.3
				end
				self.TotemBar = TotemBar
				self.TotemBar.colors = oUF.colors.totems
			end
		end
		
		if (unit~="targettarget" and unit~="focus") then
		
			local pvp = healthbar:CreateFontString(nil, "OVERLAY", "GameFontNormalSmall")
			pvp:SetPoint("BOTTOM")
			pvp:SetAlpha(.5)
			self:Tag(pvp, "|cffdd0000[pvp]|r")
		
			local icon = healthbar:CreateTexture(nil, "OVERLAY")
			icon:SetSize(22, 22)
			icon:SetPoint('TOP', 0, 8)
			self.RaidIcon = icon

			local leader = healthbar:CreateTexture(nil, "OVERLAY")
			leader:SetSize(16, 16)
			leader:SetPoint('CENTER', self, 'TOPLEFT', 4, -4)
			self.Leader = leader
		
			local resting = healthbar:CreateTexture(nil, "OVERLAY")
			resting:SetSize(32, 32)
			resting:SetPoint('CENTER', self, 'BOTTOMLEFT', 4, 4)
			self.Resting = resting
			
			local lfdrole = healthbar:CreateTexture(nil, "OVERLAY")
			lfdrole:SetSize(16, 16)
			lfdrole:SetPoint('CENTER', self, 'LEFT', 0, 0)
			self.LFDRole = lfdrole
			
			self.QuestIcon = healthbar:CreateTexture(nil, "OVERLAY")
			self.QuestIcon:SetSize(32, 32)
			self.QuestIcon:SetPoint("TOPLEFT", lfdrole, 2, 0)
		end

		if unit and (unit:find("targettarget")) then
			self:SetSize(unit:find('targettarget') and config.tot.width or config.focustot.width, unit:find('targettarget') and config.tot.height or config.focustot.height)
			powerbarString:Hide()
			healthbarString:Hide()
			name:SetPoint("RIGHT", healthbar, "RIGHT")

			self.numDebuffs = 3

			local debuffs = CreateFrame("Frame", nil, self)
			debuffs:SetSize(self:GetWidth(), self:GetHeight())
			debuffs:SetPoint("TOPLEFT", self, "BOTTOMLEFT", 3, -3)
			debuffs.size = 20
			debuffs.initialAnchor = 'TOPLEFT'
			debuffs["growth-y"] = "DOWN"
			debuffs["growth-x"] = "RIGHT"			
			
			debuffs.PostUpdateIcon = PostUpdateAuraIcon
			debuffs.PostCreateIcon = PostCreateAuraIcon
			self.Debuffs = debuffs
		end
	  
		if (unit and unit == "player") then
			self:SetSize(config.player.width, config.player.height)

			healthbarString:SetPoint("LEFT", healthbar, "LEFT", 3, 0)
			healthbarString:SetJustifyH("LEFT")

			powerbarString:SetPoint("TOPRIGHT", healthbar, "TOPRIGHT", -3, -6)
			powerbarString:SetJustifyH("RIGHT")
		end
	  
		if (unit and unit == "pet") then
			self.numDebuffs = 0

			self:SetSize(config.pet.width, config.pet.height)
			healthbarString:Hide()
			powerbarString:Hide()
			name:SetPoint("RIGHT", healthbar, "RIGHT")

			local debuffs = CreateFrame("Frame", nil, self)
			debuffs:SetSize(30, self:GetHeight())
			debuffs:SetPoint("TOPLEFT", self, "BOTTOMLEFT", 0, -4)
			debuffs.size = 20
			debuffs.PostUpdateIcon = PostUpdateAuraIcon
			debuffs.PostCreateIcon = PostCreateAuraIcon
			self.Debuffs = debuffs
		end
		
		if(unit and unit == "target") then
			powerbarString:SetPoint("TOPRIGHT", healthbarString, "BOTTOMRIGHT", 0, 0)
			powerbarString:SetJustifyH("RIGHT")
			self:SetSize(config.target.width, config.target.height)
			
			self.numDebuffs = 40
			self.numBuffs = 10
			
			local buffs = CreateFrame("Frame", nil, self)
			buffs:SetSize(self:GetWidth(), 30)
			buffs:SetPoint("TOPLEFT", self, "TOPLEFT", 0, 30)
			
			buffs.spacing = 4
			buffs.initialAnchor = 'TOPLEFT'
			
			buffs.size = 27
			buffs.PostCreateIcon = PostCreateAuraIcon
			self.Buffs = buffs
			
			local debuffs = CreateFrame("Frame", nil, self)
			debuffs:SetSize(self:GetWidth(), 30)
			debuffs:SetPoint("TOPLEFT", self, "BOTTOMLEFT", 2, -2)
			debuffs.size = 32
			
			debuffs.showDebuffType = true
			debuffs.spacing = 4
			debuffs.initialAnchor = 'TOPLEFT'
			debuffs["growth-y"] = "DOWN"
			debuffs["growth-x"] = "RIGHT"			
			
			debuffs.PostUpdateIcon = PostUpdateAuraIcon
			debuffs.PostCreateIcon = PostCreateAuraIcon
			self.Debuffs = debuffs

			-- combo points on target
			local cpoints = {}
			cpoints.unit = PlayerFrame.unit
			for i = 1, 5 do
				cpoints[i] = self:CreateTexture(nil, "OVERLAY")
				cpoints[i]:SetSize(15, 15)
				cpoints[i]:SetTexture([[Interface\FriendsFrame\StatusIcon-Online]])
				if i == 1 then
					cpoints[i]:SetPoint("TOPRIGHT", 15, 6)
				else
					cpoints[i]:SetPoint("TOP", cpoints[i-1], "BOTTOM", 0, 3)
				end
			end
			self.CPoints = cpoints
		end
		  
		if(unit and unit == "focus" or unit:find('focustarget')) then
			self:SetSize(config.focus.width, config.focus.height)
			healthbarString:Hide()
			powerbarString:Hide()
			name:SetPoint("RIGHT", healthbar, "RIGHT")

			self.numDebuffs = 0

			local debuffs = CreateFrame("Frame", nil, self)
			debuffs:SetSize(self:GetWidth()*1.5, self:GetHeight())
			debuffs:SetPoint("BOTTOMLEFT", self, "BOTTOMRIGHT", 4, 3)
			debuffs.size = 22
			debuffs.PostUpdateIcon = PostUpdateAuraIcon
			debuffs.PostCreateIcon = PostCreateAuraIcon
			self.Debuffs = debuffs
		end
		  
		return self
	end
	  
	oUF:RegisterStyle("style", func)
	oUF:SetActiveStyle("style")
	local player  = oUF:Spawn("player", "oUF_Player")
	if UIMovableFrames then tinsert(UIMovableFrames, player) end
	local target  = oUF:Spawn("target", "oUF_Target")
	if UIMovableFrames then tinsert(UIMovableFrames, target) end
	local tot     = oUF:Spawn("targettarget", "oUF_TargetTarget")
	if UIMovableFrames then tinsert(UIMovableFrames, tot) end
	local pet     = oUF:Spawn("pet", "oUF_Pet")
	if UIMovableFrames then tinsert(UIMovableFrames, pet) end
	local focus   = oUF:Spawn("focus", "oUF_Focus")
	if UIMovableFrames then tinsert(UIMovableFrames, focus) end
	local focustarget   = oUF:Spawn("focustarget", "oUF_FocusTarget")
	if UIMovableFrames then tinsert(UIMovableFrames, focustarget) end
	
	player:SetPoint("TOPLEFT", 30, -40)
	target:SetPoint("TOPLEFT", 320, -40)
	tot:SetPoint("RIGHT", target, "LEFT", -10, 0)
	pet:SetPoint("RIGHT", player.Portrait, "LEFT", -10, 0)
	focus:SetPoint("LEFT", player, "RIGHT", 10, 0)
	focustarget:SetPoint("TOPLEFT", focus, "BOTTOMLEFT", 0, -3)
	
	-- thanks Tukz for dropdowns tables
	do
		UnitPopupMenus["SELF"] = { "PVP_FLAG", "LOOT_METHOD", "LOOT_THRESHOLD", "OPT_OUT_LOOT_TITLE", "LOOT_PROMOTE", "DUNGEON_DIFFICULTY", "RAID_DIFFICULTY", "RESET_INSTANCES", "RAID_TARGET_ICON", "SELECT_ROLE", "CONVERT_TO_PARTY", "CONVERT_TO_RAID", "LEAVE", "CANCEL" };
		UnitPopupMenus["PET"] = { "PET_PAPERDOLL", "PET_RENAME", "PET_ABANDON", "PET_DISMISS", "CANCEL" };
		UnitPopupMenus["PARTY"] = { "MUTE", "UNMUTE", "PARTY_SILENCE", "PARTY_UNSILENCE", "RAID_SILENCE", "RAID_UNSILENCE", "BATTLEGROUND_SILENCE", "BATTLEGROUND_UNSILENCE", "WHISPER", "PROMOTE", "PROMOTE_GUIDE", "LOOT_PROMOTE", "VOTE_TO_KICK", "UNINVITE", "INSPECT", "ACHIEVEMENTS", "TRADE", "FOLLOW", "DUEL", "RAID_TARGET_ICON", "SELECT_ROLE", "PVP_REPORT_AFK", "RAF_SUMMON", "RAF_GRANT_LEVEL", "CANCEL" }
		UnitPopupMenus["PLAYER"] = { "WHISPER", "INSPECT", "INVITE", "ACHIEVEMENTS", "TRADE", "FOLLOW", "DUEL", "RAID_TARGET_ICON", "RAF_SUMMON", "RAF_GRANT_LEVEL", "CANCEL" }
		UnitPopupMenus["RAID_PLAYER"] = { "MUTE", "UNMUTE", "RAID_SILENCE", "RAID_UNSILENCE", "BATTLEGROUND_SILENCE", "BATTLEGROUND_UNSILENCE", "WHISPER", "INSPECT", "ACHIEVEMENTS", "TRADE", "FOLLOW", "DUEL", "RAID_TARGET_ICON", "SELECT_ROLE", "RAID_LEADER", "RAID_PROMOTE", "RAID_DEMOTE", "LOOT_PROMOTE", "RAID_REMOVE", "PVP_REPORT_AFK", "RAF_SUMMON", "RAF_GRANT_LEVEL", "CANCEL" };
		UnitPopupMenus["RAID"] = { "MUTE", "UNMUTE", "RAID_SILENCE", "RAID_UNSILENCE", "BATTLEGROUND_SILENCE", "BATTLEGROUND_UNSILENCE", "RAID_LEADER", "RAID_PROMOTE", "RAID_MAINTANK", "RAID_MAINASSIST", "RAID_TARGET_ICON", "LOOT_PROMOTE", "RAID_DEMOTE", "RAID_REMOVE", "PVP_REPORT_AFK", "CANCEL" };
		UnitPopupMenus["VEHICLE"] = { "RAID_TARGET_ICON", "VEHICLE_LEAVE", "CANCEL" }
		UnitPopupMenus["TARGET"] = { "RAID_TARGET_ICON", "CANCEL" }
		UnitPopupMenus["ARENAENEMY"] = { "CANCEL" }
		UnitPopupMenus["FOCUS"] = { "RAID_TARGET_ICON", "CANCEL" }
		UnitPopupMenus["BOSS"] = { "RAID_TARGET_ICON", "CANCEL" }
	end	
end
tinsert(SuperClassic.modules, module) -- finish him!